package com.gec.system.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gec.model.system.Category;
import com.gec.system.service.CategoryService;
import com.gec.system.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "分类管理系统")
@RestController
@RequestMapping("/admin/movie/category") //隔日请求加上父路径
public class CategoryController {
    @Autowired
    private CategoryService categoryService;
    // 分页
    @ApiOperation("分页")
    @GetMapping("/{page}/{limit}")
    public Result selectCategoryPageByVo(@PathVariable Long page,
                                       @PathVariable Long limit)
    {
        IPage<Category> iPage = new Page<>(page,limit);

        iPage =   this.categoryService.selectPage(iPage);

        return Result.ok(iPage);
    }
    // 添加影视
    @ApiOperation("添加")
    @PostMapping("/addCategory")
    public Result addMember(@RequestBody Category category) {
        boolean b = this.categoryService.save(category);
        if (b)
        {
            return Result.ok();
        }
        else
        {
            return Result.fail();
        }
    }
    // 根据id 去获取一个用户
    @ApiOperation("根据id 去获取一个影视")
    @GetMapping("/findCategoryById/{id}")
    public Result findMemberById(@PathVariable Long id)
    {
        Category category = this.categoryService.getById(id);
        return Result.ok(category);
    }
    @ApiOperation("修改实现")
    @PostMapping("/updateCategory")
    public Result updateMember(@RequestBody Category category)
    {
        boolean b = this.categoryService.updateById(category);
        if (b)
        {
            return Result.ok();
        }
        else
        {
            return Result.fail();
        }
    }
    // 删除
    @ApiOperation("删除")
    @DeleteMapping("/removeCategoryById/{id}")
    public Result removeBatchMemberByIds(@PathVariable Long id)
    {
        boolean b = this.categoryService.removeById(id);
        if (b)
        {
            return Result.ok();
        }
        else
        {
            return Result.fail();
        }
    }
    // 批量删除
    @ApiOperation("批量删除")
    @DeleteMapping("/batchRemove")
    public Result batchRemove(@RequestBody List<Long> ids)
    {
        boolean isSuccess = this.categoryService.removeByIds(ids);
        if (isSuccess)
        {
            return Result.ok();
        }
        else
        {
            return Result.fail();
        }
    }
}
